import MapBoxUI = layaUI.main.MapBoxUI;
import GlowFilter = laya.filters.GlowFilter;
import Point = Laya.Point;

class MapBox extends MapBoxUI{
    private mapImageList:Array<any> = [this.map1,this.map2,this.map3,this.map4,this.map5,this.map6];
    private qzImageList:Array<any> = [this.qz1,this.qz2,this.qz3,this.qz4,this.qz5,this.qz6];
    private hgImageList:Array<any> = [this.hg1,this.hg2,this.hg3,this.hg4,this.hg5,this.hg6];

    private armsImageArray:Array<string> = ["main/arms_1.png","main/arms_2.png","main/arms_3.png","main/arms_4.png",
        "main/arms_5.png","main/arms_6.png","main/arms_7.png","main/arms_8.png","main/arms_9.png","main/arms_10.png"];
    private map1Array:Array<any> = [
        [15,13],[66,14],[116,18],[175,17],[13,62],[65,63],[119,63],[167,65],[14,114],[72,114],[129,116],[187,126],
        [69,174],[128,175],[196,179],[94,236],[165,229],[219,230],[282,236],[86,288],[142,283],[197,282],[246,284],
        [302,285],[99,337],[153,333],[276,331],[131,384]
    ];
    private map2Array:Array<any> = [
        [12,208],[8,263],[9,331],[9,378],[52,401],[10,428],[86,436],[49,460],[7,484],[193,417],[243,402],[265,446],
        [223,461],[170,464],[100,485],[59,516],[12,547],[29,591],[93,564],[145,517],[208,511],[265,509],[311,531],
        [373,552],[336,577],[257,565],[205,560],[149,564],[122,607],[213,602]
    ];
    private map3Array:Array<any> = [
        [285,9],[250,48],[295,54],[259,93],[304,98],[283,144],[335,161],[393,8],[375,48],[371,95],[460,7],[428,41],
        [417,86],[469,58],[547,5],[513,40],[552,51],[417,157],[472,129],[516,94],[514,140],[562,99],[606,42],[640,10],
        [689,13],[647,60],[608,91]
    ];
    private map4Array:Array<any> = [
        [370,228],[411,227],[391,270],[461,192],[452,234],[436,274],[401,316],[371,345],[439,347],[461,310],[486,263],
        [503,220],[537,196],[532,254],[581,206],[622,203],[590,261],[513,299],[493,350],[533,339],[590,316],[567,357],
        [506,422],[328,415],[328,463],[374,469],[441,443],[506,422],[413,487]
    ];
    private map5Array:Array<any> = [
        [635,150],[658,121],[690,170],[693,103],[720,71],[754,45],[773,8],[815,24],[764,97],[726,135],[773,139],[807,71],
        [822,117],[767,180],[785,223],[655,255],[680,288],[708,252],[759,259],[744,300],[786,320],[818,252],[856,196],
        [859,88],[856,39],[895,9],[924,89],[899,139],[901,192],[917,280],[919,324],[923,376]
    ];
    private map6Array:Array<any> = [
        [489,475],[461,511],[461,552],[643,456],[588,438],[556,465],[524,504],[608,485],[806,555],[651,363],[614,387],
        [720,502],[671,502],[628,523],[568,519],[508,546],[461,552],[595,557],[550,566],[521,598],[477,593],[427,603],
        [716,412],[756,443],[811,509],[674,399],[699,360],[746,379],[794,419],[834,448],[574,596],[711,563],[754,556],
        [806,555],[857,511],[893,462],[853,554],[906,537],[769,597],[693,603],[824,600],[880,598],[920,584]
    ];
    private mapXYOffset:Array<any> = [
        [0,0],[0,171],[209,0],[305,176],[574,0],[382,326]
    ];
    private allArsmArray:Array<any> = [];
    private mapArray:Array<any> = [this.map1Array,this.map2Array,this.map3Array,this.map4Array,this.map5Array,this.map6Array];

    private static glowFilter:Array<any> = [new GlowFilter("#ffff00", 10, 0, 0)];//发光滤镜
    public static oldID:number = -1;
    private overImage:Laya.Image;//经过的图片
    private isWin:boolean = false;//是否胜利
    private isJS:boolean = false;//是否结束
    private addFlag:Array<number> = [];//兵种mapId标记
    private model:MapModel;//数据

    constructor() {
        super();
        //添加侦听
        this.map1.on(Laya.Event.CLICK,this,this.mapClickHandler);
        this.map2.on(Laya.Event.CLICK,this,this.mapClickHandler);
        this.map3.on(Laya.Event.CLICK,this,this.mapClickHandler);
        this.map4.on(Laya.Event.CLICK,this,this.mapClickHandler);
        this.map5.on(Laya.Event.CLICK,this,this.mapClickHandler);
        this.map6.on(Laya.Event.CLICK,this,this.mapClickHandler);
        this.on(Laya.Event.MOUSE_MOVE,this,this.mouseMoveHandler);
        this.on(Laya.Event.MOUSE_OUT,this,this.mouseOutHandler);
        //数据
        this.model = MapModel.getInstance();
        //文字字体
        this.txtArea.style.align = "center";
        this.txtArea.style.leading = 10;
        //cdn加载
        ResUtils.setLoadSkin(this.map3);
        ResUtils.setLoadSkin(this.map5);
        ResUtils.setLoadSkin(this.map6);
    }

    /**
     * 添加侦听
     */
    public addEvent():void{
        //刷新显示
        this.updateView();
    }

    /**
     * 移除侦听
     */
    public removeEvent():void{
        //清空全部兵种信息
        this.clearArmsAll();
    }

    /**
     * 刷新显示
     */
    public updateView():void{
        //刷新自己新占领的弹框
        this.updateNewOccupy();
        //刷新胜利|结算信息
        this.updateWinOrResultInfo();
        //根据占领情况添加兵种信息
        this.updateArmsInfo();
        //刷新旗帜信息
        this.updateQzInfo();
        //刷新皇冠信息
        this.updateHgInfo();
    }

    //刷新自己新占领的弹框
    private updateNewOccupy():void{
        if(MapBox.oldID == MapModel.getInstance().newOwnID && MapBox.oldID != -1){
            Alert.getInstance().showMsg = LangUtils.getText(12);
            Alert.getInstance().open();
            MapBox.oldID = -1;
        }
    }

    //刷新胜利|结算信息
    private updateWinOrResultInfo():void{
        //重置数据
        this.isJS = false;
        this.isWin = false;
        this.boxTxt.visible = false;
        //检测是否有人胜利
        var haveWin:boolean = this.model.lastWinAddress != Web3Manager.EMPTY_ACCOUNT && this.model.lastWinAddress != null;
        if(haveWin == false){
            //检测土地是否已经全部占满
            if(MapModel.getInstance().occupyNum >= 6){
                this.isJS = true;
                //显示结算信息
                this.showBoxTxt(21);
            }
        }else{
            this.isWin = true;
            //显示胜利信息
            this.showBoxTxt(5);
        }
    }

    //显示文本信息 胜利|结算
    private showBoxTxt(txtId:number):void{
        this.boxTxt.visible = true;
        var desc:string = LangUtils.getText(txtId);
        if(txtId == 5){
            desc = desc.replace("[1]"," " + Utils.getShortAddress(MapModel.getInstance().lastWinAddress) + " ");
        }
        this.txtArea.innerHTML = desc;
        this.txtArea.y = (this.bg2.height - this.txtArea.contextHeight) / 2;
        //清空选中土地
        this.clearOverMap();
    }

    //鼠标移动
    private mouseMoveHandler():void{
        //检测是否可以鼠标事件
        if(this.checkCanMouse() == false){
            return;
        }
        //检测地图经过
        this.checkOverMap();
    }

    //鼠标移开
    private mouseOutHandler():void{
        //检测是否可以鼠标事件
        if(this.checkCanMouse() == false){
            return;
        }
        //清空选中土地
        this.clearOverMap();
    }

    //地图点击
    private mapClickHandler(evt:Laya.Event):void{
        //检测是否可以鼠标事件
        if(this.checkCanMouse() == false){
            return;
        }
        //清空选中土地
        this.clearOverMap();
        //弹出购买框
        MapByAlert.getInstance().id = this.mapImageList.indexOf(evt.currentTarget);
        MapByAlert.getInstance().open();
    }

    //检测是否可以鼠标事件
    private checkCanMouse():boolean{
        //检测是否胜利 || 有遮罩 || 结算
        return !(this.model.nowCount == 0 || this.isWin || MainMaskView.getInstance().inStage() || this.isJS);
    }

    //检测over到的土地
    private checkOverMap():void{
        //清空选中土地
        this.clearOverMap();
        //计算over
        var mouseX:number = Laya.stage.mouseX;
        var mouseY:number = Laya.stage.mouseY;
        for (var i = 0; i < this.mapImageList.length; i++) {
            var mapImage:Laya.Image = this.mapImageList[i];
            if(mapImage == null || mapImage.source == null){
                continue;
            }
            //获取local位置
            var point:Point = (mapImage.parent as Laya.Sprite).globalToLocal(new Point(mouseX,mouseY));
            //获取像素透明
            var array:Array<any> = mapImage.source.getPixels(point.x-mapImage.x,point.y-mapImage.y,1,1);
            if(array == null || array[0] == 0){
                continue;
            }
            //设置选中土地
            this.setOverMap(mapImage);
            //地图接缝重合
            break;
        }
    }

    //设置选中土地
    private setOverMap(mapImage:Laya.Image):void{
        this.overImage = mapImage;
        this.boxMap.addChild(this.bg);
        this.boxMap.addChild(mapImage);
        this.bg.visible = true;
        //设置滤镜集合为发光滤镜
        mapImage.filters =  MapBox.glowFilter;
    }

    //清空选中
    private clearOverMap():void{
        if(this.overImage != null){
            this.overImage.filters = [];
            this.bg.visible = false;
        }
    }

    //刷新自己的旗帜信息
    private updateQzInfo():void{
        var ownIdList:Array<number> = MapModel.getInstance().ownIdList;
        if(ownIdList == null){
            return;
        }
        var qzImage:Laya.Image;
        for(var i:number=0;i<this.qzImageList.length;i++){
            qzImage = this.qzImageList[i];
            qzImage.visible = ownIdList.indexOf(i) != -1 && this.isWin == false;
            qzImage.parent.addChild(qzImage);
        }
    }

    //刷新皇冠信息
    private updateHgInfo():void{
        var occupyIdList:Array<number> = MapModel.getInstance().occupyIdList;
        if(occupyIdList == null){
            return;
        }
        var hgImage:Laya.Image;
        for(var i:number=0;i<this.hgImageList.length;i++){
            hgImage = this.hgImageList[i];
            hgImage.visible = this.model.lastWinIndex == i && this.isWin == true;
            hgImage.parent.addChild(hgImage);
        }
    }

    //根据占领情况添加兵种信息
    private updateArmsInfo():void{
        var occupyIdList:Array<number> = MapModel.getInstance().occupyIdList;
        if(occupyIdList == null){
            return;
        }
        //检测是否胜利
        if(this.isWin == true){
            for (var i:number=0;i<this.mapImageList.length;i++){
                //胜利 选中土地
                if(i == this.model.lastWinIndex){
                    this.setOverMap(this.mapImageList[i]);
                    //添加兵种图标
                    this.addArmsByMapId(i);
                }else{
                    //失败 清空兵种图标
                    this.clearArmsByMapId(i);
                }
            }
        }else{
            for(var i:number=0;i<this.mapImageList.length;i++){
                if(occupyIdList.indexOf(i) == -1){
                    //清空兵种图标
                    this.clearArmsByMapId(i);
                }else{
                    //添加兵种图标
                    this.addArmsByMapId(i);
                }
            }
        }
    }

    //添加兵种图标根据土地id
    private addArmsByMapId(type:number):void{
        if(this.addFlag.indexOf(type) != -1)return;
        //随机需要添加的兵种个数
        var armsNum:number = Math.floor(Math.random()*5+10);
        //随机兵种位置信息
        var armsIndexArray:Array<number> = this.getArmsIndexArray(armsNum);
        //随机兵种类型
        var armsTypeArray:Array<number> = this.getArmsTypeArray(armsNum);
        //添加兵种
        var armsInfo:Array<Laya.Image> = [];
        for (var i = 0; i < armsIndexArray.length; i++) {
            var armsImage:Laya.Image = new Laya.Image();
            var imgIndex:number = armsTypeArray[i];
            armsImage.skin = this.armsImageArray[imgIndex];
            var armsXYIndex:number = armsIndexArray[i];
            var temp:Array<any> = this.mapArray[type];
            var xyOffset:Array<any> = this.mapXYOffset[type]
            var xy:Array<any> = temp[armsXYIndex];
            if(xy){
                armsImage.x = xy[0] - xyOffset[0];
                armsImage.y = xy[1] - xyOffset[1];
                this.mapImageList[type].addChild(armsImage);
                armsInfo.push(armsImage);
            }
        }
        this.addFlag.push(type);
        this.allArsmArray[type] = armsInfo;
    }

    //获取兵种位置信息
    private getArmsIndexArray(armsNum:number):Array<any>{
        var armsIndexArray:Array<number> = [];
        var min:number = 0;
        var max:number = this.map1Array.length-1;
        var index:number;
        for (var i = 0; i < armsNum; i++) {
            index = Math.floor(Math.random()*(max-min+1)+min);
            if(armsIndexArray.indexOf(index) == -1){
                armsIndexArray.push(index);
            }else{
                i --;
            }
        }
        return armsIndexArray;
    }

    //获取兵种类型
    private getArmsTypeArray(armsNum:number):Array<any>{
        var index:number;
        var armsTypeArray:Array<number> = [];
        for(var i:number=0;i<armsNum;i++) {
            index = Math.floor(Math.random()*((this.armsImageArray.length - 1)-0+1)+0);
            armsTypeArray.push(index);
        }
        return armsTypeArray;
    }

    //清理兵种图标
    private clearArmsByMapId(mapId:number):void{
        var array:Array<Laya.Image> = this.allArsmArray[mapId];
        if(array == null){
            return;
        }
        var index:number = this.addFlag.indexOf(mapId);
        if(index == -1){
            return;
        }
        for(var i:number=0;i<array.length;i++) {
            var obj:Laya.Image = array[i];
            obj.removeSelf();
        }
        this.allArsmArray[mapId] = null;
        this.addFlag.splice(index,1);
    }

    //清空全部兵种信息
    private clearArmsAll():void{
        for(var i:number=0;i<this.mapImageList.length;i++){
            this.clearArmsByMapId(i);
        }
    }
}